package com.huawei.codearts.controller;


import com.huawei.codearts.model.User;
import com.huawei.codearts.service.UserService;
import com.huawei.codearts.tools.utils.ResponseUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Clock;
import java.time.Instant;

/**
 * mybatis Mapper 模式 增删改查
 */

@Slf4j
@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;  // 通过 @RequiredArgsConstructor 注入

    @GetMapping
    public String getAllUsers() {
        String result = ResponseUtils.response();
        try {
            result = ResponseUtils.response(userService.getAllUsers());
        } catch(Exception e) {
            // 处理 Class.forName 错误log
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @GetMapping("/{id}")
    public String getUserById(@PathVariable Long id) {
        String result = ResponseUtils.response();
        try {
            result = ResponseUtils.response(userService.getUserById(id));
        } catch(Exception e) {
            // 处理 Class.forName 错误
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @PostMapping
    public String saveUser(@RequestBody User user) {
//        long timestamp = System.currentTimeMillis() / 1000;
        Instant instant = Clock.systemDefaultZone().instant();
        long timestamp = instant.getEpochSecond();
        user.setCreateTime(timestamp);
        String result = ResponseUtils.error("创建失败");
        try {
            // mapper XML方式 新增数据
            Long res = userService.saveUser(user);
            System.out.println("/api/users...saveUser..." + res);
            System.out.println("/api/users...saveUser...insertId..." + user.getId());  // 新插入的ID
            if (res == 1L) {
                result = ResponseUtils.ok();
            }
        } catch(Exception e) {
            // 处理 Class.forName 错误
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @PostMapping("/addUser")
    public String addUser(@RequestBody User user) {
        Instant instant = Clock.systemDefaultZone().instant();
        long timestamp = instant.getEpochSecond();
        user.setCreateTime(timestamp);
        String result = ResponseUtils.error("创建失败");
        try {
            // mapper 注解方式 新增数据
            Long res = userService.insertUserAnnotation(user);
            System.out.println("/api/users...addUser..." + res);
            System.out.println("/api/users...addUser...insertId..." + user.getId());  // 新插入的ID
            if (res == 1L) {
                result = ResponseUtils.ok();
            }
        } catch(Exception e) {
            // 处理 Class.forName 错误
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @PutMapping("/{id}")
    public String updateUser(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        String result = ResponseUtils.error("修改失败");
        try {
            Long res = userService.updateUser(user);
            System.out.println("/api/users...updateUser..." + res);
            if (res == 1L) {
                result = ResponseUtils.ok();
            }
        } catch(Exception e) {
            // 处理 Class.forName 错误
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @DeleteMapping("/{id}")
    public String deleteUser(@PathVariable Long id) {
        String result = ResponseUtils.error("删除失败");
        try {
            Long res = userService.deleteUser(id);
            System.out.println("/api/users...deleteUser..." + res);
            if (res == 1L) {
                result = ResponseUtils.ok();
            }
        } catch(Exception e) {
            // 处理 Class.forName 错误
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @PostMapping("/updateWithTx")
    public String updateWithTx(@RequestParam Long id) {
        String result = ResponseUtils.error("更新失败");
        try {
            Long res = userService.updateWithTx(id);
            System.out.println("/api/users/updateWithTx...updateWithTx..." + res);
            if (res == 1L) {
                result = ResponseUtils.ok();
            }
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @PostMapping("/springWithTx")
    public String springWithTx(@RequestParam Long id) {
        String result = ResponseUtils.error("更新失败");
        try {
            Long res = userService.springWithTx(id);
            System.out.println("/api/users/springWithTx...springWithTx..." + res);
            if (res == 1L) {
                result = ResponseUtils.ok();
            }
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

}
